package com.xms.dependency.service;

import com.xms.core.exception.XmsException;
import com.xms.core.module.ModuleCollection;
import com.xms.dependency.entity.Dependency;
import com.xms.utils.CollectionUtil;
import com.xms.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.*;

/**
 * <p>
 * DependencyBatchBuilder
 * </p>
 *
 * @author migo
 * @since 2021/10/23
 */
@Service
public class DependencyBatchBuilder implements IDependencyBatchBuilder {
    private IDependencyCreaterService _dependencyCreaterService;
    private List<Dependency> _entities;

    @Autowired
    public DependencyBatchBuilder(IDependencyCreaterService dependencyCreaterService) {
        _dependencyCreaterService = dependencyCreaterService;
        _entities = new ArrayList<>();
    }

    @Override
    public IDependencyBatchBuilder append(int dependentComponentType, UUID dependentObjectId, int requiredComponentType, UUID requiredObjectId) {
        return this.append(dependentComponentType, dependentObjectId, requiredComponentType, Arrays.asList(requiredObjectId));
    }

    @Override
    public IDependencyBatchBuilder append(String dependentComponentName, UUID dependentObjectId, String requiredComponentName, UUID requiredObjectId) {
        return this.append(ModuleCollection.getIdentity(dependentComponentName), dependentObjectId, ModuleCollection.getIdentity(requiredComponentName), requiredObjectId);
    }

    @Override
    public IDependencyBatchBuilder append(int dependentComponentType, UUID dependentObjectId, int requiredComponentType, Collection<UUID> requiredObjectId) {
        if (CollectionUtil.notEmpty(requiredObjectId)) {
            for (UUID item : requiredObjectId) {
                Dependency entity = new Dependency() {
                    {
                        setDependentcomponenttype(dependentComponentType);
                        setDependencyid(UUIDUtil.get());
                        setDependentobjectid(dependentObjectId);
                        setRequiredcomponenttype(requiredComponentType);
                        setRequiredobjectid(item);
                    }
                };
                _entities.add(entity);
            }
        }
        return this;
    }

    @Override
    public IDependencyBatchBuilder append(String dependentComponentName, UUID dependentObjectId, String requiredComponentName, Collection<UUID> requiredObjectId) {
        return this.append(ModuleCollection.getIdentity(dependentComponentName), dependentObjectId, ModuleCollection.getIdentity(requiredComponentName), requiredObjectId);
    }

    @Override
    public void clear() {
        _entities.clear();
    }

    @Override
    public boolean save() throws XmsException, SQLException {
        if (CollectionUtil.notEmpty(_entities)) {
            boolean success = _dependencyCreaterService.createMany(_entities);
            if (success) {
                this.clear();
            }
            return success;
        }
        return false;
    }
}
